﻿$(function () {
    $.kish.setBaseTables = {};
    $.extend($.kish.setBaseTables, {
        initializing: function () {
            $.kish.setBaseTables.ListCarBrands();
            $.kish.setBaseTables.ListCarColors();
            $.kish.setBaseTables.ListCarStatus();
            $.kish.setBaseTables.SetBrandDrp();

            $('.brandDrp').change(function () {
                var brandID = $(this).val();
                if (brandID != 0)
                    $.kish.setBaseTables.ListCarModels(brandID);
                else { $('.carModelTable').find("tr:gt(1)").html(''); }
            });

            $('.addBrand').click(function () {
                $.kish.setBaseTables.AddCarBrands();
            });
            $('.addColor').click(function () {
                $.kish.setBaseTables.AddCarColors();
            });
            $('.addStatus').click(function () {
                $.kish.setBaseTables.AddCarStatus();
            });
            $('.addModel').click(function () {
                $.kish.setBaseTables.AddCarModel();
            });
        },

        //*************************//
        ListCarBrands: function () {
            var table = $('.carBrandTable');
            var url = '../../KishCar/Services/CarAnn.svc/GetAllBrands';
            var delUrl = '../../KishCar/Services/CarAnn.svc/DelCarBrand';
            var upUrl = '../../KishCar/Services/CarAnn.svc/UpdateCarBrand';
            var delWarningMsgCode = 4;
            $.Jahanutility.fillTable(table, url, 'برند', delUrl, upUrl, delWarningMsgCode);
        },

        //*************************//
        ListCarColors: function () {
            var table = $('.carColorTable');
            var url = '../../KishCar/Services/CarAnn.svc/GetColor';
            var delUrl = '../../KishCar/Services/CarAnn.svc/DelCarColor';
            var upUrl = '../../KishCar/Services/CarAnn.svc/UpdateCarColor';
            var delWarningMsgCode = 5;
            $.Jahanutility.fillTable(table, url, 'رنگ', delUrl, upUrl, delWarningMsgCode);
        },

        //*************************//
        ListCarStatus: function () {
            var table = $('.carStatusTable');
            var url = '../../KishCar/Services/CarAnn.svc/GetAllStatus';
            var delUrl = '../../KishCar/Services/CarAnn.svc/DelCarStatus';
            var upUrl = '../../KishCar/Services/CarAnn.svc/UpdateCarStatus';
            var delWarningMsgCode = 6;
            $.Jahanutility.fillTable(table, url, 'وضعیت', delUrl, upUrl, delWarningMsgCode);
        },

        //*************************//
        ListCarModels: function (ID) {
            $('.carModelTable').find("tr:gt(1)").html('');
            var table = $('.carModelTable');
            var url = '../../KishCar/Services/CarAnn.svc/GetModel';
            var Header = "مدل";
            var data = JSON.stringify({ modelID: ID });
            $.ajax({
                url: url,
                data: data,
                'async': false,
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {

                    if (data['d'] != null && data['d'].length != 0) {
                        for (var i = 0; i < data['d'].length; i++) {
                            var x = '<tr id=' + data['d'][i].ID + '>'
                            + '<td ><span class="dataSpan">' + $.trim(data['d'][i].Code) + ' </span> <input type="text" class="display_none validaion inputData" /></td >'
                                + '<td><img alt="ویرایش" title="ویرایش" class="noBorder  inline-block pointer fn_edit " src="../../Images/ico/edit.gif" />'
                                    + '<img alt="ذخیره" title="ذخیره" class="noBorder display_none pointer fn_add upModel right" src="../../Images/ico/tick.png" />'
                                    + '<img alt="لغو" title="لغو" class="noBorder display_none pointer fn_cancel right" src="../../Images/ico/close.png" /></td>'
                                + '<td><img alt="حذف" title="حذف" src="../../Images/ico/delete.gif" class="noBorder inline-block pointer delModel " /></td>'
                              + '</tr>';
                            table.append(x);
                        };

                        $('.delModel').click(function () {
                            var thisRow = $(this).closest("tr");
                            $.kish.setBaseTables.DelModel(thisRow);
                        });

                        $('.fn_cancel').click(function () {
                            var thisRow = $(this).closest("tr");
                            $.Jahanutility.ShowTrElem(thisRow);
                        });

                        $('.fn_edit').click(function () {
                            var thisRow = $(this).closest("tr");
                            $.Jahanutility.EditableTr(thisRow);
                        });

                        $('.upModel').click(function () {
                            var thisRow = $(this).closest("tr");
                            $.kish.setBaseTables.UpdateModel(thisRow);
                        });
                    }
                }
            });
        },

        //*************************//
        DelModel: function (thisRow) {
            var url = '../../KishCar/Services/CarAnn.svc/DelCarModel';
            var ObjectToDelID = thisRow.attr('id');
            $.kish.message.getConfirmationMessage(7, function () {
                $.ajax({
                    'type': 'POST',
                    'url': url,
                    'data': JSON.stringify({ "ID": ObjectToDelID }),
                    'contentType': 'application/json; charset=utf-8',
                    'dataType': 'json',
                    'success': function (result) {
                        if (result['d'].IsSucceed) {
                            $(thisRow).remove();
                            //$.kish.message.showUserMessage(result['d'].Message.Message);
                        }
                        else {
                            $.kish.message.showErrorMessage(result['d'].Message.Message);
                        }
                    },
                    'error': function () {
                        $.kish.message.alertError("خطا در سیستم");
                    }
                });

            }, function () {
                // $this.show();
            });
        },

        //*************************//
        UpdateModel: function (thisRow) {
            var url = '../../KishCar/Services/CarAnn.svc/UpdateCarModel';
            var UpOjb = {};
            var ObjectToUpID = thisRow.attr('id');
            var data = $.trim(thisRow.find('.inputData').val());
            var lastData = thisRow.find('.dataSpan').html();

            if (!data)
                return false;
            if (lastData == data)
                $.Jahanutility.ShowTrElem(thisRow, data);

            UpOjb.ID = ObjectToUpID;
            UpOjb.Code = data;
            UpOjb.BrandID = $('.brandDrp').val();

            $.ajax({
                'type': 'POST',
                'url': url,
                'data': JSON.stringify({ "model": UpOjb }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (result) {
                    if (result['d'].IsSucceed) {
                        $.Jahanutility.ShowTrElem(thisRow, data);
                    }
                    else {
                        $.kish.message.showErrorMessage(result['d'].Message.Message);
                    }
                },
                'error': function () {
                    $.kish.message.alertError("خطا در سیستم");
                }
            });

        },

        //*************************//
        AddCarModel: function () {
            $('.drpValidator').each(function () {
                $(this).triggerHandler("focusout");
            });

            if ($('.carModelTable .error').size() > 0) {
                $('.error').effect('shake', { times: 3 }, 50);
                return false;
            }
            var code = $.trim($('.modelCodeTxt').val());
            var url = '../../KishCar/Services/CarAnn.svc/AddCarModel';
            var brandID = $('.brandDrp').val();
            var modelToAdd = {};
            modelToAdd.Code = code;
            modelToAdd.ID = 0;
            modelToAdd.BrandID = brandID;

            if (!code)
                return false;
            $.ajax({
                'type': 'POST',
                'url': url,
                'data': JSON.stringify({ "model": modelToAdd }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (result) {
                    if (result['d'].IsSucceed) {
                        $('.modelCodeTxt').val('');
                        $('.carModelTable').find("tr:gt(1)").html('');
                        $.kish.setBaseTables.ListCarModels(brandID);
                    }
                    else {
                        $.kish.message.showErrorMessage(result['d'].Message.Message);
                    }
                },
                'error': function () {
                    $.kish.message.alertError("خطا در سیستم");
                }
            });
        },

        //*************************//
        AddCarBrands: function () {
            var code = $.trim($('.brandCodeTxt').val());
            var url = '../../KishCar/Services/CarAnn.svc/AddCarBrand';
            var brandToAdd = {};
            brandToAdd.Code = code;
            if (!code)
                return false;
            $.ajax({
                'type': 'POST',
                'url': url,
                'data': JSON.stringify({ "DTO": brandToAdd }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (result) {
                    if (result['d'].IsSucceed) {
                        $('.brandCodeTxt').val('');
                        $('.carBrandTable').find("tr:gt(0)").html('');
                        $.kish.setBaseTables.ListCarBrands();
                        $.kish.setBaseTables.SetBrandDrp();
                    }
                    else {
                        $.kish.message.showErrorMessage(result['d'].Message.Message);
                    }
                },
                'error': function () {
                    $.kish.message.alertError("خطا در سیستم");
                }
            });
        },

        //*************************//
        AddCarColors: function () {
            var code = $.trim($('.colorCodeTxt').val());
            var url = '../../KishCar/Services/CarAnn.svc/AddCarColor';
            var colorToAdd = {};
            colorToAdd.Code = code;
            if (!code)
                return false;
            $.ajax({
                'type': 'POST',
                'url': url,
                'data': JSON.stringify({ "DTO": colorToAdd }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (result) {
                    if (result['d'].IsSucceed) {
                        $('.colorCodeTxt').val('');
                        $('.carColorTable').find("tr:gt(0)").html('');
                        $.kish.setBaseTables.ListCarColors();
                    }
                    else {
                        $.kish.message.showErrorMessage(result['d'].Message.Message);
                    }
                },
                'error': function () {
                    $.kish.message.alertError("خطا در سیستم");
                }
            });
        },

        //*************************//
        AddCarStatus: function () {
            var code = $.trim($('.statusCodeTxt').val());
            var url = '../../KishCar/Services/CarAnn.svc/AddCarStatus';
            var statusToAdd = {};
            statusToAdd.Code = code;
            if (!code)
                return false;
            $.ajax({
                'type': 'POST',
                'url': url,
                'data': JSON.stringify({ "DTO": statusToAdd }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (result) {
                    if (result['d'].IsSucceed) {
                        $('.statusCodeTxt').val('');
                        $('.carStatusTable').find("tr:gt(0)").html('');
                        $.kish.setBaseTables.ListCarStatus();
                    }
                    else {
                        $.kish.message.showErrorMessage(result['d'].Message.Message);
                    }
                },
                'error': function () {
                    $.kish.message.alertError("خطا در سیستم");
                }
            });
        },
        //********************************//
        SetBrandDrp: function () {
            $('.brandDrp').html('');
            var brandDrp = $('.brandDrp');
            var url = '../../KishCar/Services/CarAnn.svc/GetAllBrands';
            var data = '';
            var topOption = 'برند';
            $.Jahanutility.fillDrp(brandDrp, url, data, topOption);
        }



    });
    $.kish.setBaseTables.initializing();
});